<template>
  <div class="rankings">
    <!-- 页面标题 -->
    <div class="page-header">
      <div class="container">
        <h1 class="page-title">排行榜</h1>
        <p class="page-subtitle">发现最受欢迎的目的地、游记和旅行者</p>
      </div>
    </div>

    <div class="container">
      <div class="row">
        <!-- 排行榜导航 -->
        <div class="col-12">
          <div class="ranking-tabs">
            <el-tabs v-model="activeTab" @tab-change="handleTabChange">
              <el-tab-pane label="热门目的地" name="destinations">
                <template #label>
                  <span><i class="bi bi-geo-alt"></i> 热门目的地</span>
                </template>
              </el-tab-pane>
              <el-tab-pane label="精选游记" name="posts">
                <template #label>
                  <span><i class="bi bi-journal-text"></i> 精选游记</span>
                </template>
              </el-tab-pane>
              <el-tab-pane label="活跃用户" name="users">
                <template #label>
                  <span><i class="bi bi-people"></i> 活跃用户</span>
                </template>
              </el-tab-pane>
              <el-tab-pane label="热门攻略" name="strategies">
                <template #label>
                  <span><i class="bi bi-map"></i> 热门攻略</span>
                </template>
              </el-tab-pane>
            </el-tabs>
          </div>
        </div>

        <!-- 排行榜内容 -->
        <div class="col-lg-8">
          <div class="ranking-content">
            <!-- 目的地排行榜 -->
            <div v-if="activeTab === 'destinations'" class="ranking-list">
              <div
                v-for="(item, index) in destinationRankings"
                :key="item._id"
                class="ranking-item"
              >
                <div class="ranking-number">
                  <span :class="getRankClass(index + 1)">{{ index + 1 }}</span>
                </div>
                <div class="item-image">
                  <img :src="item.image" :alt="item.name" />
                </div>
                <div class="item-info">
                  <h4 class="item-title">{{ item.name }}</h4>
                  <p class="item-desc">{{ item.description }}</p>
                  <div class="item-stats">
                    <span><i class="bi bi-journal-text"></i> {{ item.reviewCount }} 条评论</span>
                    <span><i class="bi bi-eye"></i> {{item.views}} 次浏览</span>
                    <span><i class="bi bi-heart"></i> {{item.votes }} 次点赞</span>
                  </div>
                </div>
                <div class="item-score">
                  <div class="score-number">{{ item.score }}</div>
                  <div class="score-label">热度</div>
                </div>
              </div>
            </div>

            <!-- 游记排行榜 -->
            <div v-if="activeTab === 'posts'" class="ranking-list">
              <div
                v-for="(post, index) in postRankings"
                :key="post.id"
                class="ranking-item"
              >
                <div class="ranking-number">
                  <span :class="getRankClass(index + 1)">{{ index + 1 }}</span>
                </div>
                <div class="item-image">
                  <img :src="post.cover" :alt="post.title" />
                </div>
                <div class="item-info">
                  <h4 class="item-title">{{ post.title }}</h4>
                  <p class="item-desc">{{ post.summary }}</p>
                  <div class="item-meta">
                    <span class="author">by {{ post.author.username }}</span>
                    <span class="location"><i class="bi bi-geo-alt"></i> {{ post.location }}</span>
                  </div>
                  <div class="item-stats">
                    <span><i class="bi bi-eye"></i> {{ formatNumber(post.viewCount) }}</span>
                    <span><i class="bi bi-heart"></i> {{ formatNumber(post.likeCount) }}</span>
                    <span><i class="bi bi-chat"></i> {{ post.commentCount }}</span>
                  </div>
                </div>
                <div class="item-score">
                  <div class="score-number">{{ post.score }}</div>
                  <div class="score-label">评分</div>
                </div>
              </div>
            </div>

            <!-- 用户排行榜 -->
            <div v-if="activeTab === 'users'" class="ranking-list">
              <div
                v-for="(user, index) in userRankings"
                :key="user.id"
                class="ranking-item"
              >
                <div class="ranking-number">
                  <span :class="getRankClass(index + 1)">{{ index + 1 }}</span>
                </div>
                <div class="item-image">
                  <img :src="user.avatar" :alt="user.username" />
                </div>
                <div class="item-info">
                  <h4 class="item-title">{{ user.username }}</h4>
                  <p class="item-desc">{{ user.bio }}</p>
                  <div class="item-stats">
                    <span><i class="bi bi-journal-text"></i> {{ user.postCount }} 篇游记</span>
                    <span><i class="bi bi-people"></i> {{ user.followerCount }} 粉丝</span>
                    <span><i class="bi bi-heart"></i> {{ formatNumber(user.totalLikes) }} 获赞</span>
                  </div>
                </div>
                <div class="item-score">
                  <div class="score-number">{{ user.score }}</div>
                  <div class="score-label">影响力</div>
                </div>
              </div>
            </div>

            <!-- 攻略排行榜 -->
            <div v-if="activeTab === 'strategies'" class="ranking-list">
              <div
                v-for="(strategy, index) in strategyRankings"
                :key="strategy.id"
                class="ranking-item"
              >
                <div class="ranking-number">
                  <span :class="getRankClass(index + 1)">{{ index + 1 }}</span>
                </div>
                <div class="item-image">
                  <img :src="strategy.cover" :alt="strategy.title" />
                </div>
                <div class="item-info">
                  <h4 class="item-title">{{ strategy.title }}</h4>
                  <p class="item-desc">{{ strategy.summary }}</p>
                  <div class="item-meta">
                    <span class="author">by {{ strategy.author.username }}</span>
                    <el-rate v-model="strategy.rating" disabled size="small" />
                  </div>
                  <div class="item-stats">
                    <span><i class="bi bi-eye"></i> {{ formatNumber(strategy.viewCount) }}</span>
                    <span><i class="bi bi-bookmark"></i> {{ strategy.collectCount }}</span>
                    <span><i class="bi bi-chat"></i> {{ strategy.commentCount }}</span>
                  </div>
                </div>
                <div class="item-score">
                  <div class="score-number">{{ strategy.score.toFixed(1) }}</div>
                  <div class="score-label">评分</div>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 侧边栏 -->
        <div class="col-lg-4">
          <div class="sidebar">
            <!-- 本周热门 -->
            <div class="sidebar-card">
              <h4 class="card-title">
                <i class="bi bi-fire"></i> 本周热门
              </h4>
              <div class="hot-list">
                <div
                  v-for="(item, index) in weeklyHot"
                  :key="item.id"
                  class="hot-item"
                >
                  <span class="hot-rank">{{ index + 1 }}</span>
                  <div class="hot-info">
                    <div class="hot-title">{{ item.title }}</div>
                    <div class="hot-stats">{{ formatNumber(item.viewCount) }} 浏览</div>
                  </div>
                </div>
              </div>
            </div>

            <!-- 数据统计 -->
            <div class="sidebar-card">
              <h4 class="card-title">
                <i class="bi bi-bar-chart"></i> 数据统计
              </h4>
              <div class="stats-grid">
                <div class="stat-item">
                  <div class="stat-number">{{ totalStats.destinations.toLocaleString() }}</div>
                  <div class="stat-label">目的地</div>
                </div>
                <div class="stat-item">
                  <div class="stat-number">{{ totalStats.posts.toLocaleString() }}</div>
                  <div class="stat-label">游记</div>
                </div>
                <div class="stat-item">
                  <div class="stat-number">{{ totalStats.users.toLocaleString() }}</div>
                  <div class="stat-label">用户</div>
                </div>
                <div class="stat-item">
                  <div class="stat-number">{{ totalStats.strategies.toLocaleString() }}</div>
                  <div class="stat-label">攻略</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElTabs, ElTabPane, ElRate } from 'element-plus'
import { defaultAvatar, images } from '@/assets/images'
import { getSpots } from '@/api/data'
import type { 
  DestinationRanking,
  PostRanking,
  UserRanking,
  StrategyRanking,
  HotItem,
  TotalStats
} from '@/types/rankings'

// 响应式数据
const activeTab = ref('destinations')

// 各类排行榜数据
const destinationRankings = ref<DestinationRanking[]>([])
const postRankings = ref<PostRanking[]>([])
const userRankings = ref<UserRanking[]>([])
const strategyRankings = ref<StrategyRanking[]>([])
const weeklyHot = ref<HotItem[]>([])

// 总体统计
const totalStats = reactive<TotalStats>({
  destinations: 1580,
  posts: 89600,
  users: 125800,
  strategies: 12450
})

// 标签页切换
const handleTabChange = (tabName: string) => {
  activeTab.value = tabName
}

// 获取排名样式
const getRankClass = (rank: number) => {
  if (rank === 1) return 'rank-first'
  if (rank === 2) return 'rank-second'
  if (rank === 3) return 'rank-third'
  return 'rank-normal'
}

// 格式化数字
const formatNumber = (num: number) => {
  if (num >= 10000) {
    return (num / 10000).toFixed(1) + 'w'
  } else if (num >= 1000) {
    return (num / 1000).toFixed(1) + 'k'
  }
  return num.toString()
}
const getdata = async () => {
  try {
    const res = await getSpots()
    console.log('API响应数据结构:', res.data)
destinationRankings.value=res.data
  } catch (error) {
    console.error('请求出错:', error)
    throw error
  }
}
// 初始化数据
const initData = () => {
  getdata()
  // 目的地排行榜
  // destinationRankings.value = Array.from({ length: 10 }, (_, index) => ({
  //   id: index + 1,
  //   name: `热门目的地 ${index + 1}`,
  //   description: '这里有着令人惊叹的自然风光和丰富的文化底蕴...',
  //   image: `/src/assets/iamges/${['20221106190735.jpg', '90.jpg', '980.jpg', 'OIP.webp'][index % 4]}`,
  //   postCount: Math.floor(Math.random() * 1000) + 500,
  //   viewCount: Math.floor(Math.random() * 100000) + 50000,
  //   likeCount: Math.floor(Math.random() * 10000) + 5000,
  //   score: Math.floor(Math.random() * 30) + 70
  // }))

  // 游记排行榜
  postRankings.value = Array.from({ length: 10 }, (_, index) => ({
    id: index + 1,
    title: `精彩游记标题 ${index + 1}`,
    summary: '这是一次难忘的旅行体验，分享给大家...',
    cover: [images.img20221106190735, images.img90, images.img980, images.imgOIP][index % 4],
    location: `目的地 ${index + 1}`,
    author: {
      username: `旅行者${index + 1}`
    },
    viewCount: Math.floor(Math.random() * 50000) + 10000,
    likeCount: Math.floor(Math.random() * 5000) + 1000,
    commentCount: Math.floor(Math.random() * 500) + 100,
    score: (Math.random() * 2 + 3).toFixed(1)
  }))

  // 用户排行榜
  userRankings.value = Array.from({ length: 10 }, (_, index) => ({
    id: index + 1,
    username: `旅行达人${index + 1}`,
    bio: '热爱旅行的自由行者，分享旅途中的美好时光',
    avatar: defaultAvatar,
    postCount: Math.floor(Math.random() * 100) + 50,
    followerCount: Math.floor(Math.random() * 10000) + 1000,
    totalLikes: Math.floor(Math.random() * 50000) + 10000,
    score: Math.floor(Math.random() * 500) + 200
  }))

  // 攻略排行榜
  strategyRankings.value = Array.from({ length: 10 }, (_, index) => ({
    id: index + 1,
    title: `实用攻略指南 ${index + 1}`,
    summary: '详细的旅行攻略，包含实用信息和小贴士...',
    cover: [images.img20221106190735, images.img90, images.img980, images.imgOIP][index % 4],
    author: {
      username: `攻略专家${index + 1}`
    },
    rating: 4 + Math.random(),
    viewCount: Math.floor(Math.random() * 30000) + 5000,
    collectCount: Math.floor(Math.random() * 3000) + 500,
    commentCount: Math.floor(Math.random() * 300) + 50,
    score: 4 + Math.random()
  }))

  // 本周热门
  weeklyHot.value = Array.from({ length: 5 }, (_, index) => ({
    id: index + 1,
    title: `本周热门内容 ${index + 1}`,
    viewCount: Math.floor(Math.random() * 10000) + 5000
  }))
}

onMounted(() => {
  initData()
})
</script>

<style scoped>
.page-header {
  background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%);
  color: white;
  padding: 60px 0;
  text-align: center;
}

.page-title {
  font-size: 3rem;
  font-weight: 700;
  margin-bottom: 10px;
}

.page-subtitle {
  font-size: 1.2rem;
  opacity: 0.9;
  margin: 0;
}

.ranking-tabs {
  background: white;
  border-radius: 15px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  margin-bottom: 30px;
  overflow: hidden;
}

.ranking-tabs :deep(.el-tabs__header) {
  margin: 0;
}

.ranking-content {
  background: white;
  border-radius: 15px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.ranking-list {
  padding: 20px;
}

.ranking-item {
  display: flex;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #f0f0f0;
  transition: all 0.3s;
}

.ranking-item:hover {
  background: #f8f9fa;
  transform: translateX(5px);
}

.ranking-item:last-child {
  border-bottom: none;
}

.ranking-number {
  width: 60px;
  text-align: center;
}

.ranking-number span {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 1.1rem;
}

.rank-first {
  background: linear-gradient(45deg, #ffd700, #ffed4a);
  color: white;
}

.rank-second {
  background: linear-gradient(45deg, #c0c0c0, #e2e8f0);
  color: white;
}

.rank-third {
  background: linear-gradient(45deg, #cd7f32, #d69e2e);
  color: white;
}

.rank-normal {
  background: #f1f5f9;
  color: #64748b;
}

.item-image {
  width: 80px;
  height: 80px;
  border-radius: 10px;
  overflow: hidden;
  margin: 0 20px;
}

.item-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.item-info {
  flex: 1;
}

.item-title {
  font-size: 1.1rem;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.item-desc {
  color: #666;
  font-size: 14px;
  line-height: 1.5;
  margin-bottom: 10px;
}

.item-meta {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-bottom: 10px;
  font-size: 14px;
}

.author {
  color: #667eea;
  font-weight: 500;
}

.location {
  color: #666;
}

.item-stats {
  display: flex;
  gap: 15px;
  font-size: 13px;
  color: #888;
}

.item-stats span {
  display: flex;
  align-items: center;
  gap: 4px;
}

.item-score {
  text-align: center;
  min-width: 80px;
}

.score-number {
  font-size: 1.5rem;
  font-weight: bold;
  background: linear-gradient(45deg, #ff9a9e, #fecfef);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.score-label {
  font-size: 12px;
  color: #999;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.sidebar-card {
  background: white;
  border-radius: 15px;
  padding: 20px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.card-title {
  font-size: 1.1rem;
  font-weight: 600;
  color: #333;
  margin-bottom: 15px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.card-title i {
  color: #ff9a9e;
}

.hot-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.hot-item {
  display: flex;
  align-items: center;
  gap: 12px;
}

.hot-rank {
  width: 24px;
  height: 24px;
  background: linear-gradient(45deg, #ff9a9e, #fecfef);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: bold;
}

.hot-info {
  flex: 1;
}

.hot-title {
  font-size: 14px;
  color: #333;
  margin-bottom: 4px;
}

.hot-stats {
  font-size: 12px;
  color: #999;
}

.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 15px;
}

.stat-item {
  text-align: center;
  padding: 15px;
  background: #f8f9fa;
  border-radius: 10px;
}

.stat-number {
  font-size: 1.5rem;
  font-weight: bold;
  background: linear-gradient(45deg, #ff9a9e, #fecfef);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.stat-label {
  font-size: 12px;
  color: #666;
  margin-top: 5px;
}

@media (max-width: 768px) {
  .page-header {
    padding: 40px 0;
  }
  
  .page-title {
    font-size: 2.2rem;
  }
  
  .ranking-item {
    flex-direction: column;
    text-align: center;
    gap: 15px;
  }
  
  .item-image {
    margin: 0;
  }
  
  .item-meta,
  .item-stats {
    justify-content: center;
  }
  
  .sidebar {
    margin-top: 30px;
  }
}
</style>


